package com.sniper.hive;

import java.sql.*;

/**
 * Created by suzhen on 2017/6/15.
 */
public class HiveHolder {

    private Connection conn;
    private String driverName = "org.apache.hive.jdbc.HiveDriver";
    private String url = "jdbc:hive2://192.168.190.118:10000/default";
    private String user = "scott";
    private String password = "tiger";

    public Connection getConn() {
        try {
            Class.forName(driverName);
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

        return conn;
    }

    public static void main(String[] args) throws SQLException {

        HiveHolder hiveHolder = new HiveHolder();
        Connection conn = hiveHolder.getConn();
        Statement statement = conn.createStatement();

        String tableName = "sniper_demo";
        statement.execute("DROP TABLE IF EXISTS " + tableName);
        statement.execute("create table " + tableName + " (key int, value string)");

        String sql = "SHOW TABLES '" + tableName + "'";
        ResultSet set2 = statement.executeQuery(sql);
        while (set2.next()) {
            System.out.println(set2.getString(1));
        }
        statement.close();
        conn.close();
    }

}
